﻿using DMC.Core.Models;
using DMC.Core.Models.Dto;
using DMC.Core.Models.Queries;
using DMC.Core.Models.VO;
using DMC.Core.Providers;
using MiniFox.Components;
using MiniFox.Data;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using MiniFox.Utility;

namespace DMC.Data
{
    [AutoResolve]
    public class ExcelImportProvider : EntityDataProvider<PlatformDbContext>, IExcelImportProvider
    {
        public ResultObject<IEnumerable<ExcelImportVo>> Query(PagedQueryObject<ExcelImportQuery> request)
        {
            PagedResultSet<DtExcelImport> result = this.PagedQueryExecute<DtExcelImport>(request);
            ResultObject<IEnumerable<ExcelImportVo>> response = result.GetResultObject(e => new ExcelImportVo
            {
                Name = e.Name,
                Template = e.Template,
                DataWriter = e.DataWriter,
                MaxImportFileSize = e.MaxImportFileSize,
                PreImport = e.PreImport,
                Sheets = e.Sheets.ToJsonObject<ExcelImportSheet[]>(),
                CreateTime = e.CreateTime,
                UpdateTime = e.UpdateTime,
                Modules = e.Modules.ToJsonObject<Dictionary<string, string>>()
            });
            return response;
        }

        public ExcelImportVo Item(string name)
        {
            var e = this.CurrentDbContext.DtExcelImport.SingleOrDefault(e => e.Name == name);
            if (e == null) return null;

            return new ExcelImportVo
            {
                Name = e.Name,
                Template = e.Template,
                DataWriter = e.DataWriter,
                MaxImportFileSize = e.MaxImportFileSize,
                PreImport = e.PreImport,
                Sheets = e.Sheets.ToJsonObject<ExcelImportSheet[]>(),
                CreateTime = e.CreateTime,
                UpdateTime = e.UpdateTime,
                Modules = e.Modules.ToJsonObject<Dictionary<string, string>>()
            };
        }

        public bool Update(ExcelImportDto dto)
        {
            var e = this.CurrentDbContext.DtExcelImport.SingleOrDefault(e => e.Name == dto.Name);
            if (e == null)
                return false;

            e.Template = dto.Template;
            e.DataWriter = dto.DataWriter;
            e.PreImport = dto.PreImport;
            e.MaxImportFileSize = dto.MaxImportFileSize;
            e.UpdateTime = DateTime.Now;
            e.Sheets = dto.Sheets.ToJsonString();
            e.Modules = dto.Modules.ToJsonString();
            return (this.Update(e) > 0);
        }

        public bool Create(ExcelImportDto dto)
        {
            var e = new DtExcelImport
            {
                Name = dto.Name,
                Template = dto.Template,
                DataWriter = dto.DataWriter,
                PreImport = dto.PreImport,
                MaxImportFileSize = dto.MaxImportFileSize,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                Sheets = dto.Sheets.ToJsonString(),
                Modules = dto.Modules.ToJsonString()
            };
            return (this.Insert(e) > 0);
        }

        public bool Remove(string name)
        {
            var e = this.CurrentDbContext.DtExcelImport.SingleOrDefault(e => e.Name == name);
            if (e == null) return false;

            return this.Delete(e) > 0;
        }
    }
}
